<template>
  <s-layout
      title="我的"
      tabbar="/pages/index/user"
      navbar="custom"
      :bgStyle="template.style?.background"
      :navbarStyle="template.style?.navbar"
      onShareAppMessage
      :showFloatButton="true"
  >
    <view class="basemap"></view>
    <s-block
        v-for="(item, index) in template.data"
        :key="index"
        :styles="item.style"
        class="shadow-effect"
    >
      <view
          v-if="item.type == 'menuGrid'"
          style="
          font-size: 30rpx;
          font-weight: 700;
          margin-left: 20rpx;
          margin-top: 14rpx;
        "
      >菜单栏
      </view
      >
      <s-block-item
          :type="item.type"
          :data="item.data"
          :styles="item.style"
          @click="handleBlockItemClick(item)"
      />
    </s-block>
    <!-- 点餐订单入口 -->
    <view
        class="catering-order-entry"
        @click="goToCateringOrders"
    >
      <view class="entry-icon">
        <uni-icons type="shop-filled" size="24" color="#fe5348"></uni-icons>
      </view>
      <view class="entry-content">
        <text class="entry-title">我的点餐订单</text>
        <text class="entry-subtitle">查看所有餐厅订单</text>
      </view>
      <view class="entry-arrow">
        <uni-icons type="forward" size="20" color="#999"></uni-icons>
      </view>
    </view>

    <view
        v-if="userInfo.employee == 1"
        style="
        width: 90%;
        background-color: white;
        margin: auto;
        padding: 20rpx;
        border-radius: 20rpx;
      "
        @click="sheep.$router.go('/pages/shoopbackground/index')"
    >工作台
    </view
    >
  </s-layout>
</template>

<script setup>
import {computed, ref} from "vue";
import {onShow, onPullDownRefresh, onLoad} from "@dcloudio/uni-app";
import sheep from "@/sheep";
import {showAuthModal} from "@/sheep/hooks/useModal";

// 隐藏原生tabBar
uni.hideTabBar();

const userInfo = computed(() => sheep.$store("user").userInfo);

const template = computed(() => sheep.$store("app").template.user);
const isLogin = computed(() => sheep.$store("user").isLogin);

console.log("kankan", userInfo.value);

const phone = ref();

onShow(() => {
  // 用户登录但是没有手机号的
  // if (phone.value == null && isLogin.value) {
  //   console.log("需要弹框");
  //   handleLogin();
  // }

  sheep.$store("user").updateUserData();
});

onLoad(() => {
  // 用户修改头像
  console.log("userInfo:", userInfo.value.nickname);
  if (userInfo.value.nickname == "微信用户") {
    uni.showModal({
      title: "提示",
      content: "点击右侧白色图标修改昵称头像",
      showCancel: false,
    });
  }
});

onPullDownRefresh(() => {
  sheep.$store("user").updateUserData();
  setTimeout(function () {
    uni.stopPullDownRefresh();
  }, 800);
});

// 处理 s-block-item 的点击事件
function handleBlockItemClick(item) {
  if (!isLogin.value) {
    // 用户未登录，弹出登录框
    handleLogin();
  } else {
    // 用户已登录，执行 s-block-item 的默认行为

    console.log('item: ', item);
    sheep.$router.go(item.data?.path, item.data?.query);
  }
}

// 弹出登录框
function handleLogin() {
  if (sheep.$platform.name === "WechatMiniProgram") {
    showAuthModal("wechatMiniLogin");
  } else {
    showAuthModal("smsLogin");
  }
}

// 跳转到点餐订单列表（显示所有餐厅的订单）
function goToCateringOrders() {
  if (!isLogin.value) {
    // 用户未登录，弹出登录框
    handleLogin();
    return;
  }
  
  console.log('【个人中心】跳转到点餐订单列表 - 显示所有餐厅订单');
  
  // 不传递任何餐厅参数，显示所有点餐订单
  sheep.$router.go('/pages/catering/order-list', {
    cateringName: '全部餐厅'
  });
}
</script>

<style lang="scss" scoped>
.basemap {
  background: linear-gradient(to bottom, #fe5348, #fd8154);
  position: absolute;
  height: 450rpx;
  width: 100%;
  z-index: -10;
}

.catering-order-entry {
  width: 90%;
  background-color: white;
  margin: 20rpx auto;
  padding: 30rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
  transition: all 0.3s;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.08);
  }
  
  .entry-icon {
    width: 80rpx;
    height: 80rpx;
    background: linear-gradient(135deg, #fff5f5 0%, #ffe8e8 100%);
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 24rpx;
  }
  
  .entry-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    
    .entry-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 8rpx;
    }
    
    .entry-subtitle {
      font-size: 24rpx;
      color: #999;
    }
  }
  
  .entry-arrow {
    opacity: 0.4;
  }
}
</style>
